package com.example.auth.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.auth.domain.UserPhone;

import com.example.auth.mapper.UserPhoneMapper;
import com.example.auth.service.UserPhoneService;
import com.example.config.exception.GlobalExcetion;
import com.example.util.sql.DBSql;
import com.example.util.sql.IdUtils;
import com.example.util.sql.SqlGenerator;
import com.example.util.sql.SqlResult;
import com.example.util.tableshard.TableShardingAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.util.Date;


/**
* @author CETC
* @description 针对表【user_phone_0】的数据库操作Service实现
* @createDate 2025-11-01 16:17:14
*/
@Service
public class UserPhoneServiceImpl extends ServiceImpl<UserPhoneMapper, UserPhone>
    implements UserPhoneService {

    /**
     * 表名前缀
     */
    private final static String USER_PHONE_TABLE_SUFFIX = "user_phone_";

    /**
     * 用户表数量
     */
    private final static Integer USER_PHONE_TABLE_COUNT=3;

    private final static String USER_PHONE_ID_SUFFIX="phone_";

    @Autowired
    private DataSource dataSource;


    private String getTableIndexByPhone(String phone) {
        if(phone==null || phone.isEmpty()){
            return "";
        }
        Integer tableIndex= TableShardingAlgorithm.getTableIndexByConsistentHash(phone,USER_PHONE_TABLE_COUNT);
        return tableIndex.toString();
    }


    private String getUserPhoneTableByPhone(String phone) {
        String index=getTableIndexByPhone(phone);
        return USER_PHONE_TABLE_SUFFIX+index;
    }


    private String createUUIdByPhone(String phone) {
        String id= IdUtils.getUUID();
        String index=getTableIndexByPhone(phone);
        return USER_PHONE_ID_SUFFIX+index+"_"+id;
    }

    private String getTableByUUID(String uuid) {
        String[] strings=uuid.split("_");
        if(strings.length < 2){
            return "";
        }
        return USER_PHONE_TABLE_SUFFIX+strings[1];
    }


    @Override
    public UserPhone getUserPhoneByPhone(String phone) {
        String tableName=getUserPhoneTableByPhone(phone);
        return this.baseMapper.getByPhone(tableName,phone);
    }

    @Override
    public UserPhone saveUserPhone(UserPhone userPhone) throws GlobalExcetion {
        if(userPhone.getUserPhone()==null || userPhone.getUserId()==null){
            throw new GlobalExcetion(1,"手机号和用户id不得为空");
        }
        String tableName=getUserPhoneTableByPhone(userPhone.getUserPhone());
        String uuid=this.createUUIdByPhone(userPhone.getUserPhone());
        userPhone.setUuid(uuid);
        userPhone.setCreateTime(new Date());
        userPhone.setUpdateTime(new Date());
        SqlResult sqlResult= SqlGenerator.generateInsert(tableName,userPhone);
        DBSql dbSql=new DBSql(dataSource);
        dbSql.executeInsert(sqlResult.getSql(),sqlResult.getParams().toArray());
        return userPhone;
    }

    @Override
    public void deleteUserPhoneByPhone(String phone) {
        String tableName=getUserPhoneTableByPhone(phone);
        this.baseMapper.deleteByPhone(tableName,phone);
    }

    @Override
    public UserPhone updateUserPhoneById(UserPhone userPhone) {
        String tableName=getUserPhoneTableByPhone(userPhone.getUserPhone());
        SqlResult sqlResult=SqlGenerator.generateUpdateById(tableName,userPhone,"uuid");
        DBSql dbSql=new DBSql(dataSource);
        userPhone.setUpdateTime(new Date());
        dbSql.executeUpdate(sqlResult.getSql(),sqlResult.getParams().toArray());
        return userPhone;
    }


}




